<?php

namespace app\index\controller;

use think\Db;
use think\File;

/* * **********************************************
 * 
 * 评论模块
 * 
 * ***********************************************
 */

class Comment extends Common {

    protected $table = 'chat_comment_tag';
    protected $log_table = 'chat_comment_log';

    //获取我的评论标签
    public function getMyCommentTag() {
        $map = [
            'l.user_id' => input('param.user_id'),
        ];

        $data = Db::table($this->log_table)
                ->alias('l')
                ->join('chat_comment_tag t', 'l.commet_id = t.id')
                ->where($map)
                ->select(['t.name' . 'l.num', 'l.commet_id']);

        return retData($data, '1000', 'ok');
    }

    //获取评论标签
    public function getCommentTag() {

        $data = Db::table($this->table)
                ->select();

        return retData($data, '1000', 'ok');
    }

    //评价某人
    public function addComment() {

        if (input('param.user_uid_callee') == input('param.user_id')) {
            return retData([], '1001', '自己不能评价自己');
        }

        $log = M('chat_usercalllog')->where(['type' => 1, 'user_uid_callee' => input('param.user_uid_callee'), 'user_uid' => input('param.user_id')])->find();

        if ($log) {
            return retData([], '1002', '没有视频记录');
        } else {

            $comment_ids = explode(',', input('param.commet_id'));
            foreach ((array) $comment_ids as $comment_id) {
                $insert = [
                    'user_id' => input('param.user_id'),
                    'commet_id' => $comment_id,
                ];
                if (Db::table($this->log_table)->where($insert)->find()) {//有的话 增加+1
                    $data = Db::table($this->log_table)->where($insert)->setInc('num', 1);
                } else {
                    $data = Db::table($this->log_table)->insert($insert);
                }
            }

            if ($data) {
                return retData([], '1000', 'ok');
            } else {
                return retData([], '1001', '发布失败');
            }
        }
    }

}
